package com.yl.chainStore.control.pay

import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.pojo.vo.pay.BalanceUpdateVo
import com.yl.chainStore.services.ICustomerInfoService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.annotations.PermsCode
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*
import java.util.*

/**
 * 用户账户信息
 */
@RestController
@RequestMapping("customer/account")
open class CustomerAccountController(
	private val iCustomerInfoService: ICustomerInfoService
) : BaseCtrl() {

	/**
	 * 获取账户
	 */
	@GetMapping("getCustomerAccount/{id}")
	@PermsCode("customer:account:view")
	@Transactional
	open fun getCustomerAccount(@PathVariable id: Long): ResponseBody {
		val customerAccount = iCustomerInfoService.getCustomerAccount(id, getContext().subCompanyId)
		return returnSuccess(customerAccount)
	}

	/**
	 * 充值
	 */
	@PostMapping("topUpBalance")
	@Transactional(rollbackFor = [Exception::class])
	open fun topUpBalance(@RequestBody vo: BalanceUpdateVo): ResponseBody {
		val topUpBalance =
			iCustomerInfoService.topUpBalance(vo.customerId, vo.money, vo.payType, TypeEnum.PAY_TYPE_TOP_UP, vo.remark,vo.giftsOfGold,null,Date())
		if (topUpBalance.bodyIsError()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return topUpBalance
		}
		return returnSuccess()
	}

	/**
	 * 退款
	 */
	@PostMapping("balanceToMoney")
	@Transactional(rollbackFor = [Exception::class])
	open fun balanceToMoney(@RequestBody vo: BalanceUpdateVo): ResponseBody {
		val balanceToMoney =
			iCustomerInfoService.balanceToMoney(vo.customerId, vo.money, TypeEnum.PAY_TYPE_BALANCE_REFUND,vo.payType, vo.remark,vo.giftsOfGold,null)
		if (balanceToMoney.bodyIsError()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return balanceToMoney
		}
		return returnSuccess()
	}

	/**
	 * 转账
	 */
	@PostMapping("transfer")
	@Transactional(rollbackFor = [Exception::class])
	open fun transfer(@RequestBody vo: BalanceUpdateVo): ResponseBody {
		if(vo.toCustomerId == null){
			return returnError(ErrorCode.PARAM_EMPTY,"请选择接收人")
		}
		val balanceToOtherCustomer =
			iCustomerInfoService.balanceToOtherCustomer(vo.customerId, vo.toCustomerId,vo.money, vo.remark,vo.giftsOfGold)
		if (balanceToOtherCustomer.bodyIsError()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return balanceToOtherCustomer
		}
		return returnSuccess()
	}

	/**
	 * 用户充值支付类型
	 */
	@GetMapping("getPayTypeCustomerType")
	open fun getCustomerType(): ResponseBody {
		val typeList = mutableListOf<Map<String, Any>>()
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_MONEY.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_ALIPAY.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_WECHAT.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_WORD_OF_MOUTH.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_BANK_CARD.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_ADVANCE_PAYMENT.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_SOCIAL_SECURITY_CARD.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_CITY_HEALTH_CARD.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_PROVINCIAL_HEALTH_CARD.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_PROVINCIAL_TRANSFER.getMap())
		typeList.add(TypeEnum.OTHER.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_TIDE_SHARE.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_PING_AN_GOOD_DOCTOR.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_MEITUAN.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_LETOOTH_MESH.getMap())
		typeList.add(TypeEnum.CUSTOMER_PAY_TYPE_NANCHANG_LINE.getMap())
		return returnSuccess(typeList)
	}


}
